package com.book.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.book.common.BaseController;
import com.book.entity.BookInfo;
import com.book.entity.BookType;
import com.book.entity.Teacher;
import com.book.utils.BeanUtils;
import com.book.utils.FTPTool;
import com.github.drinkjava2.jsqlbox.ActiveRecord;
import com.github.drinkjava2.jsqlbox.DB;
import com.github.drinkjava2.jsqlbox.entitynet.EntityNet;
import com.github.drinkjava2.jsqlbox.handler.EntityNetHandler;


/**
*
*     小说信息控制类
*
*/
@Controller 
public class InfoController  extends BaseController<BookInfo> {

	
	  private static final Object[] targets = new Object[] { new EntityNetHandler(), BookInfo.class,
	  BookType.class,DB.giveBoth("bi", "bt") };
	 
	@Autowired
	private FTPTool ftpTool;
	
	public InfoController() {
		super();
	}
	
	public String index(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		System.out.println("进入 InfoController.java   的方法");
		
		
		EntityNet net = getRecord().autoNet(BookInfo.class,BookType.class);
		List<BookInfo> infolist =net.pickEntityList(BookInfo.class);
		 
		req.setAttribute("datalist", infolist);
		
		
		return String.format("/%s/list", getModule());
	}
	
	public String save(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		
		BookInfo eo = (BookInfo) getRecord();
	
	// 创建文件上传核心类
			ServletFileUpload sfu = new ServletFileUpload(new DiskFileItemFactory());
			sfu.setHeaderEncoding("UTF-8");// 设置编码
	// 处理表单请求
		    List<FileItem> itemList = sfu.parseRequest(req);
			
	for(FileItem item : itemList) {
		String fieldName = item.getFieldName();
		if(item.isFormField()) {
			//如果是普通表单文本数据
			String value = item.getString();
			BeanUtils.setProperty(eo, fieldName, new String(value.getBytes("ISO-8859-1"),"UTF-8")); 
			System.out.println(String.format("Name = %s; Value = %s", fieldName,value));
		}else {
			String imgUrl = "/" + System.nanoTime() + ".jpg";
			try {
				ftpTool.connect();
				ftpTool.uploadFileFromIS(imgUrl, item.getInputStream());
			
				BeanUtils.setProperty(eo, "imgUrl", imgUrl); 
				System.out.println(String.format("Name = %s; Value = %s", "imgUrl",imgUrl));
			
			
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
	}		
		
	
	System.out.println(eo.toString());
	
	Object pkvalue =  eo.getField(getPKFielName());
	
	
	try {
	  //判断当前是新增 还是修改   
	if(pkvalue != null && ((Integer)pkvalue)>0) {
			//修改
			eo.update();
	}else {
		//新增
		eo.insert();
	   }		 
	}catch (Exception e) {
		e.printStackTrace();
		if(e.getMessage().contains("Duplicate entry")) {
			//有条目重复
			String msg = e.getMessage();
			int beginIndex = msg.indexOf("for key")+7;
			int endIndex = msg.indexOf("Query:");
			String key = msg.substring(beginIndex,endIndex);
			return String.format("%s重复，请重新填写", key);
		}else {
			return "更新失败，请联系管理员检查日志";
		}
		
		
	}	
			return "更新成功！";
}

	public ActiveRecord<BookInfo>  getRecord() {
		return new BookInfo();
	}
	
	public String  getPKFielName() {
		return "bookId";
	}
	
	@Override
	public String getModule() {
		return "info";
	}
}
